package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.*;
import com.atguigu.jxc.entity.SaleList;
import com.atguigu.jxc.entity.SaleListGoods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.SaleListGoodsService;
import com.atguigu.jxc.vo.PCountVo;
import com.atguigu.jxc.vo.RVo;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fenge
 */
@Service
public class SaleListGoodsServiceImpl implements SaleListGoodsService {

    @Autowired
    private SaleListGoodsDao saleListGoodsDao;

    @Autowired
    private SaleListDao saleListDao;

    /*@Autowired
    private PurchaseListDao purchaseListDao;

    @Autowired
    private ReturnListDao returnListDao;

    @Autowired
    private CustomerReturnListDao customerReturnListDao;*/

    @Override
    public void save(SaleList saleList, String saleListGoodsStr, HttpSession session) {
        User user = (User) session.getAttribute("currentUser");
        Integer userId = user.getUserId();
        saleList.setUserId(userId);
        saleListDao.save(saleList);
        Gson gson = new Gson();
        List<SaleListGoods> saleListGoods = gson.fromJson(saleListGoodsStr,new TypeToken<List<SaleListGoods>>(){}.getType());
        saleListGoods.forEach(saleListGood->{
            saleListGood.setSaleListId(saleList.getSaleListId());
            saleListGoodsDao.save(saleListGood);
        });
    }

    @Override
    public Map<String, Object> list(String saleNumber, Integer customerId, Integer state, String sTime, String eTime) {
        Map<String, Object> map = new HashMap<>();
        List<SaleList> saleListGoods = saleListDao.list(saleNumber,customerId,state,sTime,eTime);
        map.put("rows",saleListGoods);
        return map;
    }

    @Override
    public Map<String, Object> goodsList(Integer saleListId) {
        List<SaleListGoods> saleListGoods = saleListGoodsDao.goodsList(saleListId);
        Map<String, Object> map = new HashMap<>();
        map.put("rows",saleListGoods);
        return map;
    }

    @Override
    public void delete(Integer saleListId) {
        List<SaleListGoods> saleListGoods = saleListGoodsDao.goodsList(saleListId);
        saleListGoods.forEach(saleListGood -> {
            saleListGoodsDao.delete(saleListGood);
        });
        saleListDao.delete(saleListId);
    }

    @Override
    public void updateState(Integer saleListId) {
        saleListDao.updateState(saleListId);

        //
    }

    @Override
    public String count(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
        List<PCountVo> pCountVos =saleListDao.count(sTime,eTime,goodsTypeId,codeOrName);
        Gson gson = new Gson();
        String s = gson.toJson(pCountVos);
        return s;
    }

    @Override
    public String getSaleDataByDay(String sTime, String eTime) {
        //损坏表,,溢出表,供应商退货进货表,用户退货买货表
        /*List<PCountVo> countIn = purchaseListDao.count(sTime, eTime, null, null);
        List<PCountVo> countOut = returnListDao.count(sTime, eTime, null, null);
        List<PCountVo> countOut1 = customerReturnListDao.count(sTime, eTime, null, null);
        List<PCountVo> countIn1 = saleListDao.count(sTime, eTime, null, null);*/
/*
SELECT SUM(sum) saleTotal,date
FROM ((SELECT SUM(amount_paid) sum,purchase_date date
FROM t_purchase_list t1
GROUP BY purchase_date)
UNION
(SELECT SUM(amount_paid) sum,sale_date date
FROM t_sale_list
GROUP BY sale_date)) tt1
GROUP BY date
 */
        List<RVo> rVos = new ArrayList<>();

        Gson gson = new Gson();
        String s = gson.toJson(rVos);
        return s;
    }
}
